/*
 * @features: 主要功能
 * : 内容说明
 * @Date: 2021-10-18 09:45:00
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-27 22:22:39
 * @LastEditors: judu233
 */

import { DEBUG } from "cc/env";
import { HotUpdate } from "../../../script/Asset/hotupdate/Hotupdate";
import UIView from "../../../script/Asset/ui/UIView";
import c3d from "../../../script/tool/cc";

/** 下载界面 */
const { ccclass, property } = c3d._decorator;

@ccclass
export default class DownloadLoading extends UIView {

    public static getPrefabUrl() {
        return "common/prefabs/DownloadLoading";
    }

    /** 下载过程中提示语 */
    private get tips() {
        return GM.i18n.trans.updatingtips;
    }

    /** 下载进度 */
    private progress: c3d.ProgressBar = null!;

    /** 下载过程中显示文字的节点 */
    private tipsLabel: c3d.Label = null!;

    /**提示tip Index */
    private tipsIndex = 0;

    /** 下载的状态 */
    private state: HotUpdate.State = null!;

    /**更新名字 */
    private updateName = "";

    comEvent = new Map([
        [HotUpdate.Event.HOTUPDATE_DOWNLOAD, this.onDownload]
    ]);

    onLoad() {
        super.onLoad();
        if (this.args) {
            this.state = this.args[0];
            this.updateName = this.args[1];
        }
        this.tipsLabel = c3d.find("tips", this.node)?.getComponent(c3d.Label);
        this.progress = c3d.find("progressBar", this.node)?.getComponent(c3d.ProgressBar);
        this.startTips();
        this.scheduleOnce(this.doUpdate, 1.5);
    }

    private doUpdate() {
        if (this.state == HotUpdate.State.TRY_DOWNLOAD_FAILED_ASSETS) {
            GM.assetMgr.hotupdate.downloadFailedAssets();
        } else {
            GM.assetMgr.hotupdate.hotUpdate();
        }
    }

    private startTips() {
        c3d.tween(this.tipsLabel.node)
            .call(() => {
                this.tipsLabel.string = this.tips[this.tipsIndex];
            })
            .delay(2)
            .call(() => {
                this.tipsIndex++;
                if (this.tipsIndex >= this.tips.length) {
                    this.tipsIndex = 0;
                }
                this.startTips();
            })
            .start();
    }

    private onDownload(info: HotUpdate.DownLoadInfo) {
        if (DEBUG) c3d.log(JSON.stringify(info));
        if (info.code == HotUpdate.Code.UPDATE_PROGRESSION) {
            this.progress.progress = info.percent == Number.NaN ? 0 : info.percent;
        } else if (info.code == HotUpdate.Code.ALREADY_UP_TO_DATE) {
            this.progress.progress = 1;
        } else if (info.code == HotUpdate.Code.UPDATE_FINISHED) {
            let tip = GM.i18n.trans.alreadyRemoteVersion.format(this.updateName);
            GM.uiMgr.tip.show(tip);
            this.close();
        } else if (info.code == HotUpdate.Code.UPDATE_FAILED ||
            info.code == HotUpdate.Code.ERROR_NO_LOCAL_MANIFEST ||
            info.code == HotUpdate.Code.ERROR_DOWNLOAD_MANIFEST ||
            info.code == HotUpdate.Code.ERROR_PARSE_MANIFEST ||
            info.code == HotUpdate.Code.ERROR_UPDATING ||
            info.code == HotUpdate.Code.ERROR_DECOMPRESS) {
            let tip = GM.i18n.trans.updateFaild.format(this.updateName);
            GM.uiMgr.tip.show(tip);
            this.close();
        }
    }
}
